import { _decorator, Component, UIOpacity, tween, Tween, Vec3 } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('AutoRotation')
export class AutoRotation extends Component {
    @property
    dt: number = 1;

    @property
    angle: number = 360;

    @property
    private isLoop: boolean = true;

    @property
    private isAwakePlay: boolean = true;

    protected start(): void {
        Tween.stopAllByTarget(this.node);

        if (this.isAwakePlay) {
            this.play();
        }
    }

    public play(): void {
        if (this.isLoop) {
            tween(this.node)
                .by(this.dt, { angle: this.angle }, { easing: 'linear' })
                .repeatForever()
                .start();
        } else {
            tween(this.node)
                .by(this.dt, { angle: this.angle }, { easing: 'linear' })
                .call(() => {
                    this.node.angle = 0;
                })
                .start();
        }
    }
}